package com.bls.blsdemo

import android.support.v7.widget.LinearLayoutManager
import android.view.KeyEvent
import android.view.View
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.ToastUtils
import com.bls.blsdemo.GaoDeMap.PioKeyWordSearchActivity
import com.bls.blsdemo.MultiThread.ThreadActivity
import com.bls.blsdemo.PermissionDemo.PermissionActivity
import com.bls.blsdemo.QQInfoPointDemo.QQInfoPointActivity
import com.bls.blsdemo.model.RootBean
import com.bls.blsdemo.networkdemo.NetWorkActivity
import com.bls.blsdemo.rxdemo.CheeseActivity
import com.bls.blsdemo.tablayoutdemo.SimpleHomeActivity
import com.bls.lib.ui.activity.BaseActivity
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import kotlinx.android.synthetic.main.activity_root.*
import java.util.ArrayList

/**
 *
 * 　　  () 　　　  ()
 * 　　  ( ) 　　　( )
 * 　　  ( ) 　　　( )
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　┻　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ——————————————————
 * Created by BinglingSu on 2017/11/9.
 */
class RootActivity : BaseActivity(), BaseQuickAdapter.OnItemChildClickListener {

    private var rootAdapter: RootAdapter? = null
    private val rootChildrenList = ArrayList<RootBean.RootChildren>()

    override fun getLayoutId(): Int = R.layout.activity_root
    override fun initMain() {

    }

    override fun initView() {
        rv_list.layoutManager = LinearLayoutManager(this)
        rootAdapter = RootAdapter(rootChildrenList)
        rv_list.adapter = rootAdapter
    }

    override fun initListener() {
        rootAdapter?.onItemChildClickListener = this
    }

    override fun initData() {
        getRootListData()
        rootAdapter?.setNewData(rootChildrenList)
    }


    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {
        /* val targetActivity: Class<*>? = when (position) {
             0,1,2 -> rootChildrenList[position].clazz
             else -> SplashActivity::class.java
         }
         targetActivity?.let { ActivityUtils.startActivity(it) }*/
        ActivityUtils.startActivity(rootChildrenList[position].clazz)
    }


    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK && event?.repeatCount == 0) {
            exit()
            return true
        }
        return super.onKeyDown(keyCode, event)
    }

    private var mExitTime: Long = 0;
    private fun exit() {
        if (System.currentTimeMillis() - mExitTime > 2000) {
            ToastUtils.showShort("再按一次退出应用")
            mExitTime = System.currentTimeMillis();
        } else {
            AppUtils.exitApp()
        }
    }

    class RootAdapter(data: List<RootBean.RootChildren>?) : BaseQuickAdapter<RootBean.RootChildren, BaseViewHolder>(R.layout.view_root_list, data) {
        override fun convert(helper: BaseViewHolder, item: RootBean.RootChildren) {
            helper.setText(R.id.tv_title, item.title).setText(R.id.tv_message, item.message).addOnClickListener(R.id.ll_root)
        }
    }


    private fun getRootListData() {
        rootChildrenList.add(RootBean.RootChildren("RxDemo", "我也看不懂这个", CheeseActivity::class.java))
        rootChildrenList.add(RootBean.RootChildren("NetWorkDemo", "网络请求", NetWorkActivity::class.java))
        rootChildrenList.add(RootBean.RootChildren("TabLayoutDemo", "导航栏，包含两种模式", SimpleHomeActivity::class.java))
        rootChildrenList.add(RootBean.RootChildren("QQPointDemo", "QQ可拖动小圆点指示器", QQInfoPointActivity::class.java))
        rootChildrenList.add(RootBean.RootChildren("ThreadDemo", "线程，Handler，相关", ThreadActivity::class.java))
        rootChildrenList.add(RootBean.RootChildren("PermissionDemo", "权限，设置头像，相关", PermissionActivity::class.java))
        rootChildrenList.add(RootBean.RootChildren("高德地图Demo", "模糊搜索", PioKeyWordSearchActivity::class.java))
        //rootChildrenList.add(RootBean.RootChildren("DataBinding Demo", "DataBinding Demo", DataBindingActivity::class.java))

    }
}